<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/12/29
 * Time: 13:59
 * 启动广告
 */
namespace app\province\controller;
use think\Db;
class Startover extends Base {

    /*******
     * @return mixed|\think\response\Json
     * 首页
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $map['b_user'] = session('p_uid');
            $count = Db::table('up_banner_start')->alias('a')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_banner_start')
                ->alias ('a')
                ->page($page, $limit)
                ->where($map)
                ->select();
            foreach ($list as $key=>&$vo){
                    $p_username = Db::table('up_province')->where('p_id',$vo['b_user'])->value('p_username');
                    $vo['b_user'] =$p_username;
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /*****
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('param.id');
        $res=  Db::table('up_banner_start')->where('b_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    /******
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(request()->isPost()){
            $data['b_name'] = input('param.b_name');
            $data['b_type'] = input('param.b_type');
            $data['b_url'] = input('param.img_file');
            if(input('param.publish')==1){//设置发送时间，证明没有发布
                $data['b_status'] = 0;
                $data['b_publish_time'] = input('param.start');
                if(input('param.start')==''){
                    return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能为空']);
                }else{
                    if($data['b_publish_time']<date('Y-m-d H:i:s',time())){
                        return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能小于当前时间']);
                    }
                }
            }else{
                $data['b_status'] = 1;
                $data['b_publish_time'] = date('Y-m-d H:i:s',time());
            }
            if(input('param.b_type')==3){
                $data['b_mp_url'] = input('param.lay-img');
            }
            $data['b_update_time']=date('Y-m-d H:i:s',time());
            $data['b_time'] = date('Y-m-d H:i:s',time());
            $data['b_user'] = session('p_uid');
            $res =  Db::table('up_banner_start')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }


    public function update(){
        if(request()->isPost()){
            $id  = input('param.id');
            $data['b_name'] = input('param.b_name');
            $data['b_type'] = input('param.b_type');
            if(input('param.img_file')!=''){
                $data['b_url'] = input('param.img_file');
            }
            if(input('param.publish')==1){//设置发送时间，证明没有发布
                $data['b_status'] = 0;
                $data['b_publish_time'] = input('param.start');
                if(input('param.start')==''){
                    return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能为空']);
                }else{
                    if($data['b_publish_time']<date('Y-m-d H:i:s',time())){
                        return json(['code' => 100, 'data' => '', 'msg' => '发布时间不能小于当前时间']);
                    }
                }
            }else{
                $data['b_status'] = 1;
                $data['b_publish_time'] = date('Y-m-d H:i:s',time());
            }
            if(input('param.b_type')==3){
                $data['b_mp_url'] = input('param.lay-img');
            }
            $data['b_update_time']=date('Y-m-d H:i:s',time());
            $data['b_user'] = session('p_uid');
            $res =  Db::table('up_banner_start')->where('b_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id  = input('param.id');
        $res = Db::table('up_banner_start')->where('b_id',$id)->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
}